/**
 * 保存cookie
 * 
 * @param {*} key 
 * @param {*} value 
 * @param {过期时间毫秒}} expire 
 */
function saveCookie(key, value, expire) {
    if(expire){
        let date = new Date()
        let time = date.getTime() + expire
        date.setTime(time)
    
        document.cookie = `${key}=${value};expires=${date}`
    }else{
        document.cookie = `${key}=${value}`
    }
   
}
//获取cookie值
function getCookie(key) {
    let cookie = document.cookie //username=admin; age=20
    let arr = cookie.split(';') //['username'='admin',' age'=20]
    let value = ''
    for(let i = 0; i < arr.length;i++){
        let item =  arr[i] //username=admin
        let cookieArr = item.split('=') // [' username',admin]
        if(cookieArr[0].trim() == key){
            value = cookieArr[1]
            break;
        }
    }
    return value

}
//删除cookie
function removeCookie(key) {
    saveCookie(key,'',-1)
}